package com.example.hospital.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.example.client.common.DictFeignClient;
import com.example.common.util.constant.Constants;
import com.example.common.util.constant.HospitalConstants;
import com.example.common.util.core.resultbody.AjaxResult;
import com.example.common.util.utils.DateUtils;
import com.example.common.util.utils.StringUtils;
import com.example.hospital.respository.HospitalRepository;
import com.example.hospital.service.HospitalService;
import com.example.hospital.service.HospitalSetService;
import com.example.model.domain.entity.hospital.Hospital;
import com.example.model.vo.hospital.HospitalQueryVo;
import com.example.service.util.MD5;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Author juzipi
 * @Date 2021/7/10 11:10
 * @Info
 */
@Service
public class HospitalServiceImpl implements HospitalService {

    @Autowired
    private HospitalRepository hospitalRepository;

    @Autowired
    private HospitalSetService hospitalSetService;

    @Autowired
    private DictFeignClient dictFeignClient;


    /**
     * 校验签名
     * @param hospitalSign
     * @param hospitalCode
     * @return
     */
    @Override
    public Boolean compareHospitalSign(Object hospitalSign, Object hospitalCode) {
        //这里是有两张hospitalSet表，注意两张表的sign_key要一致，这个hospitalManage项目会请求这的接口，
        //它会把sign_key加密后发送过来，这里再给数据库里的sign_key查出来加密对比就行了
        String sign = hospitalSetService.selectSignByHospitalSetCode(hospitalCode.toString());
        String encrypt = MD5.encrypt(sign);
        return Objects.equals(encrypt,hospitalSign);
    }


    /**
     * 保存医院
     * @param parameterMap
     * @return
     */
    @Override
    public Boolean insertHospital(Map<String, Object> parameterMap) {
        //先转为String, 再用parseObject方法 传入相应的参数转为hospitalEntity对象
        String string = JSONObject.toJSONString(parameterMap);
        Hospital hospital = JSONObject.parseObject(string, Hospital.class);

        Hospital hospitalExists = hospitalRepository.getHospitalByHoscode(hospital.getHoscode());
        if (StringUtils.isNull(hospitalExists)){
            hospital.setCreateTime(DateUtils.getNowDate());
            hospital.setUpdateTime(hospital.getCreateTime());
            hospital.setStatus(HospitalConstants.HOSPITAL_STATUS_VALUE);
            hospital.setDeleted(HospitalConstants.DELETED_VALUE_FALSE);
            Hospital save = hospitalRepository.save(hospital);
            System.out.println(save);
            return true;
        }
        hospitalExists.setUpdateTime(DateUtils.getNowDate());
        hospitalRepository.save(hospitalExists);
        return true;
    }

    /**
     * 查询医院
     * @param hospitalCode
     * @return
     */
    @Override
    public Hospital getHospitalByHoscode(String hospitalCode) {
        return hospitalRepository.getHospitalByHoscode(hospitalCode);
    }

    /**
     * 查询医院分页
     * @param pageNum
     * @param pageSize
     * @param hospitalQueryVo
     * @return
     */
    @Override
    public PageInfo<Hospital> getHospitalPage(Integer pageNum, Integer pageSize, HospitalQueryVo hospitalQueryVo) {
        //条件匹配器
        ExampleMatcher exampleMatcher = ExampleMatcher.matching().withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING);
        Hospital hospital = new Hospital();
        //把hospitalSelectVo里的值复制到hospital里
        BeanUtils.copyProperties(hospitalQueryVo, hospital);
        //分页
        PageHelper.startPage(pageNum,pageSize);
        List<Hospital> hospitals = hospitalRepository.findAll(Example.of(hospital, exampleMatcher));
        PageInfo<Hospital> hospitalPageInfo = new PageInfo<>(hospitals);
        //远程调用查询dictName接口查询出医院等级
        hospitalPageInfo.getList().forEach(this::packHospitalHostype);

        return hospitalPageInfo;
    }


    @Override
    public Boolean updateHospitalStatus(String id, Integer status) {
        Hospital hospital = hospitalRepository.getHospitalById(id);
        hospital.setStatus(status);
        hospital.setUpdateTime(DateUtils.getNowDate());
        hospitalRepository.save(hospital);
        return true;
    }


    @Override
    public Map<String, Object> getHospitalDetailById(String id) {
        HashMap<String, Object> hashMap = new HashMap<>();
        Hospital hospital = this.packHospitalHostype(hospitalRepository.getHospitalById(id));
        hashMap.put(HospitalConstants.HOSPITAL,hospital);
        hashMap.put(HospitalConstants.BOOKING_RULE,hospital.getBookingRule());
        //重复的就删了
        hospital.setBookingRule(null);
        return hashMap;
    }


    @Override
    public List<Hospital> getHospitalByHosname(String hosname) {
        return hospitalRepository.getHospitalByHosname(hosname);
    }


    @Override
    public Map<String, Object> getHospitalDetailByHoscode(String hoscode) {
        //这个方法跟getHospitalDetailById基本一致，可以优化
        HashMap<String, Object> result = new HashMap<>();
        //设置一下医院等级
        Hospital hospital = this.packHospitalHostype(this.getHospitalByHoscode(hoscode));
        result.put(HospitalConstants.HOSPITAL,hospital);
        result.put(HospitalConstants.BOOKING_RULE,hospital.getBookingRule());
        hospital.setBookingRule(null);
        return result;
    }



    /**
     * 设置医院等级
     * @param hospital
     */
    private Hospital packHospitalHostype(Hospital hospital) {
        if (StringUtils.isNotNull(hospital)){
            AjaxResult ajaxResult = dictFeignClient.getName(hospital.getHostype());
            hospital.getParam().put(HospitalConstants.HOS_TYPE,ajaxResult.get(Constants.MSG));
            //查询省，市，地区
            Object provinceCode = dictFeignClient.getName(hospital.getProvinceCode()).get(Constants.MSG);
            Object cityCode = dictFeignClient.getName(hospital.getCityCode()).get(Constants.MSG);
            Object districtCode = dictFeignClient.getName(hospital.getDistrictCode()).get(Constants.MSG);
            hospital.getParam().put(HospitalConstants.FULL_ADDRESS, provinceCode.toString() + cityCode + districtCode);
            return hospital;
        }
        return null;
    }
}
